package com.example.minzhengtong.utils

import android.graphics.drawable.Drawable
import android.text.Editable
import android.text.TextWatcher
import android.text.method.HideReturnsTransformationMethod
import android.text.method.PasswordTransformationMethod
import android.view.View
import android.widget.EditText
import com.example.minzhengtong.MyApp
import com.example.minzhengtong.R

/**
 * created by WY on 2021/4/19.
 * 为输入框添加右侧图标工具类
 * 可添加清空输入内容
 * 可添加密码显示隐藏
 */
class AddDrawableUtils(var editText: EditText) {

    /**
     * 添加输入框右侧删除内容图标
     */
    fun addClearDrawable(){
        LogUtil.d("为${editText}添加删除内容监听")
        val drawable = MyApp.context.resources.getDrawable(R.drawable.img_close)
        drawable.setBounds(0,0,drawable.minimumWidth,drawable.minimumHeight)
        editText.addTextChangedListener(object : TextWatcher{
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {

            }

            override fun afterTextChanged(s: Editable) {
                if (s.isNotEmpty()){
                    editText.setCompoundDrawables(editText.compoundDrawables[0],null,drawable,null)
                    DrawableUtil(editText,object : DrawableUtil.OnDrawableListener{
                        override fun onLeft(v: View?, left: Drawable?) {

                        }

                        override fun onRight(v: View?, right: Drawable?) {
                            editText.setText("")
                        }
                    })
                }else{
                    editText.setCompoundDrawables(editText.compoundDrawables[0],null,null,null)
                }
            }
        })
    }

    /**
     * 添加输入框右侧删除内容图标
     * 并绑定操作按钮透明度和点击
     */
    fun addClearDrawable(textView: View){
        val drawable = MyApp.context.resources.getDrawable(R.drawable.img_close)
        drawable.setBounds(0,0,drawable.minimumWidth,drawable.minimumHeight)
        if (editText.text.isNotEmpty()){
            textView.isEnabled = true
            textView.alpha = 1f
            editText.setCompoundDrawables(editText.compoundDrawables[0],null,drawable,null)
        }else{
            textView.isEnabled = false
            textView.alpha = 0.3f
        }
        LogUtil.d("为${editText}添加删除内容监听")
        editText.addTextChangedListener(object : TextWatcher{
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {

            }

            override fun afterTextChanged(s: Editable) {
                if (s.isNotEmpty()){
                    textView.isEnabled = true
                    textView.alpha = 1f
                    editText.setCompoundDrawables(editText.compoundDrawables[0],null,drawable,null)
                    DrawableUtil(editText,object : DrawableUtil.OnDrawableListener{
                        override fun onLeft(v: View?, left: Drawable?) {

                        }

                        override fun onRight(v: View?, right: Drawable?) {
                            editText.setText("")
                        }
                    })
                }else{
                    textView.isEnabled = false
                    textView.alpha = 0.3f
                    editText.setCompoundDrawables(editText.compoundDrawables[0],null,null,null)
                }
            }
        })
    }

    /**
     * 添加输入框右侧显示隐藏输入内容图标
     */
    fun addHideDrawable(){
        LogUtil.d("为${editText}添加显示隐藏监听")
        val hideDrawable = MyApp.context.resources.getDrawable(R.drawable.img_pwd_hide)
        hideDrawable.setBounds(0,0,hideDrawable.minimumWidth,hideDrawable.minimumHeight)
        val showDrawable = MyApp.context.resources.getDrawable(R.drawable.img_pwd_show)
        showDrawable.setBounds(0,0,showDrawable.minimumWidth,showDrawable.minimumHeight)
        editText.setCompoundDrawables(editText.compoundDrawables[0],null,hideDrawable,null)
        editText.transformationMethod = PasswordTransformationMethod.getInstance()
        DrawableUtil(editText,object : DrawableUtil.OnDrawableListener{
            override fun onLeft(v: View?, left: Drawable?) {

            }

            override fun onRight(v: View?, right: Drawable?) {
                if (editText.transformationMethod == HideReturnsTransformationMethod.getInstance()){
                    //设置隐藏
                    editText.transformationMethod = PasswordTransformationMethod.getInstance()
                    editText.setCompoundDrawables(editText.compoundDrawables[0],null,hideDrawable,null)
                }else{
                    //设置显示
                    editText.transformationMethod = HideReturnsTransformationMethod.getInstance()
                    editText.setCompoundDrawables(editText.compoundDrawables[0],null,showDrawable,null)
                }
                editText.setSelection(editText.text.length)
            }
        })
    }



}